MESSAGE( STATUS "DEBUG: ${${PROJECT_NAME}_SOURCE_DIR}" )

GET_FILENAME_COMPONENT( BW_BUILD_DIR "${${PROJECT_NAME}_SOURCE_DIR}" ABSOLUTE )
GET_FILENAME_COMPONENT( BW_ROOT_DIR "${BW_BUILD_DIR}/../" ABSOLUTE )
GET_FILENAME_COMPONENT( BW_CMAKE_DIR "${BW_ROOT_DIR}/cmake/" ABSOLUTE )
GET_FILENAME_COMPONENT( BW_TESTS_DIR "${BW_ROOT_DIR}/tests/" ABSOLUTE )

MESSAGE( STATUS "DEBUG: ${BW_BUILD_DIR}" )
MESSAGE( STATUS "DEBUG: ${BW_ROOT_DIR}" )
MESSAGE( STATUS "DEBUG: ${BW_CMAKE_DIR}" )

SET(RUNTIME_OUTPUT_PATH ${BW_BUILD_DIR})
SET(ARCHIVE_OUTPUT_PATH ${BW_BUILD_DIR})

INCLUDE_DIRECTORIES(
  ${BW_ROOT_DIR}
  /usr/local/xce/include
  /data/home/yuwei.mu/xce/include
  /data/www/dev/include
  /usr/local/xce-dev/include
  /usr/include/mysql++
  /usr/include/mysql
  /usr/local/mysql/include
  /usr/include
  /usr/local/include
  /opt/curl/include
  /opt/thrift-0.8.0/include
  /opt/thrift-0.8.0/include/thrift
  /home/muyuwei/gtest/gtest-1.6.0/include
)

LINK_DIRECTORIES(
  /usr/lib64/nptl
  /data/home/yuwei.mu/xce/lib
  /data/www/dev/lib
  /usr/local/xce-dev/lib
  /opt/curl/lib
  /usr/local/xce/lib
  /usr/local/mysql/lib
  /opt/thrift-0.8.0/lib
  /home/muyuwei/gtest/gtest-1.6.0/lib/.libs
)

# LINK_LIBRARIES(pthread rt log4cplus mysqlpp crypto ssl)
LINK_LIBRARIES(pthread rt)

MACRO (BW_ADD_STATIC_LIBRARY directory library)
  MESSAGE(STATUS "DEBUG: ${ARGN}")
  AUX_SOURCE_DIRECTORY("${BW_ROOT_DIR}/${directory}" ${directory}_SOURCE_FILES)

  MESSAGE(STATUS "DEBUG: ${BW_ROOT_DIR}/${directory} source files = ${${directory}_SOURCE_FILES}")
  ADD_LIBRARY(${library} STATIC ${${directory}_SOURCE_FILES})
  TARGET_LINK_LIBRARIES( ${library} ${ARGN} )
ENDMACRO (BW_ADD_STATIC_LIBRARY)

MACRO (BW_ADD_SHARED_LIBRARY directory library)
  MESSAGE(STATUS "DEBUG: ${ARGN}")
  AUX_SOURCE_DIRECTORY("${BW_ROOT_DIR}/${directory}" ${directory}_SOURCE_FILES)

  MESSAGE(STATUS "DEBUG: ${BW_ROOT_DIR}/${directory} source files = ${${directory}_SOURCE_FILES}")
  ADD_LIBRARY(${library} SHARED ${${directory}_SOURCE_FILES})
  TARGET_LINK_LIBRARIES( ${library} ${ARGN} )
ENDMACRO (BW_ADD_SHARED_LIBRARY)

MACRO (BW_ADD_EXECUTABLE directory executable)
  MESSAGE(STATUS "DEBUG: ${ARGN}")
  AUX_SOURCE_DIRECTORY("${BW_ROOT_DIR}/${directory}" ${directory}_SOURCE_FILES)

  MESSAGE(STATUS "DEBUG: ${BW_ROOT_DIR}/${directory} source files = ${${directory}_SOURCE_FILES}")
  ADD_EXECUTABLE(${executable} EXCLUDE_FROM_ALL ${${directory}_SOURCE_FILES})
  TARGET_LINK_LIBRARIES( ${executable} ${ARGN} )
ENDMACRO (BW_ADD_EXECUTABLE)

MACRO (BW_ADD_TEST_CASE test_case)
  ADD_EXECUTABLE(${test_case} EXCLUDE_FROM_ALL ${BW_TESTS_DIR}/${test_case}.cc)
  TARGET_LINK_LIBRARIES( ${test_case} ${ARGN} gtest)
ENDMACRO (BW_ADD_TEST_CASE)

MACRO (GENERATE_THRIFT_RULES directory)
  FILE(GLOB THRIFT_SOURCES
    RELATIVE "${BW_ROOT_DIR}/${directory}" 
    "${BW_ROOT_DIR}/${directory}/*.thrift")
  MESSAGE("THRIFT_SOURCES : ${THRIFT_SOURCES}")
  SET(output_dir "${BW_BUILD_DIR}/thrift-gen")

  INCLUDE_DIRECTORIES("/usr/local/include/thrift/" 
    "${output_dir}/gen-cpp/")

  FOREACH(thrift_source ${THRIFT_SOURCES})
    STRING(REGEX REPLACE "\\.thrift" "_constants.cpp" constants_cpp "${thrift_source}" )
    MESSAGE(${constants_cpp})
    SET(out_file "${output_dir}/gen-cpp/${constants_cpp}")

    STRING(REGEX REPLACE "\\.thrift" "_types.cpp" types_cpp "${thrift_source}" )
    MESSAGE(${types_cpp})
    SET(out_file "${out_file}" "${output_dir}/gen-cpp/${types_cpp}")

    STRING(REGEX REPLACE "\\.thrift" ".cpp" stub_cpp "${thrift_source}" )
    MESSAGE(${stub_cpp})
    SET(out_file "${out_file}" "${output_dir}/gen-cpp/${stub_cpp}")

    # add_dependencies(${stub_cpp} ${thrift_source})

    # skeleton代码，应该链接进来吗?
    # STRING(REGEX REPLACE "\\.thrift" "_server.skeleton.cpp" skeleton_cpp "${thrift_source}" )
    # MESSAGE(${skeleton_cpp})
    # SET(out_file "${out_file}" "${output_dir}/gen-cpp/${skeleton_cpp}")

    MESSAGE(${out_file})

    add_custom_command(OUTPUT ${out_file}
      COMMAND ${CMAKE_COMMAND} -E make_directory ${output_dir}
      # COMMAND ${CMAKE_COMMAND} -E make_directory "${output_dir}" # 不能带引号!
      COMMAND thrift -o "${output_dir}" -gen cpp "${BW_ROOT_DIR}/${directory}/${thrift_source}"
		  DEPENDS ${BW_ROOT_DIR}/${directory}/${thrift_source}
      ) 

    STRING(REGEX REPLACE "\\.thrift" "" libname "${thrift_source}" )
    MESSAGE("add library thrift_${libname} ...")
    add_library("thrift_${libname}" STATIC ${out_file}) 
    TARGET_LINK_LIBRARIES( "thrift_${libname}" thrift )
  ENDFOREACH(thrift_source ${THRIFT_SOURCES})
ENDMACRO (GENERATE_THRIFT_RULES)

GENERATE_THRIFT_RULES("thrift")

BW_ADD_STATIC_LIBRARY(pugixml pugixml)
BW_ADD_STATIC_LIBRARY(fcgi_service fcgi_service fcgi boost_thread base)
BW_ADD_STATIC_LIBRARY(base base log4cplus)
BW_ADD_STATIC_LIBRARY(sns sns pugixml base ssl)
BW_ADD_STATIC_LIBRARY(ticket ticket)
BW_ADD_STATIC_LIBRARY(redis_executor redis_executor base hiredis)
BW_ADD_STATIC_LIBRARY(mmseg mmseg)
BW_ADD_STATIC_LIBRARY(database database mysqlpp mysqlclient_r)
BW_ADD_STATIC_LIBRARY(service_arch service_arch thrift_ServiceBase)

BW_ADD_TEST_CASE(logging_test base)
BW_ADD_TEST_CASE(xiaonei_adapter_test sns)
BW_ADD_TEST_CASE(fcgi_service_test fcgi_service base)
BW_ADD_TEST_CASE(pugixml_test pugixml)
BW_ADD_TEST_CASE(mmseg_test mmseg)
BW_ADD_TEST_CASE(object_pool_test base boost_thread)
BW_ADD_TEST_CASE(simple_db_pool_test database base pthread)
BW_ADD_TEST_CASE(redis_executor_test redis_executor)
BW_ADD_TEST_CASE(redis_protocol_test redis_executor)
BW_ADD_TEST_CASE(serialize_test thrift_ArticleComment)
BW_ADD_TEST_CASE(redis_comment_adapter_test article_comment_service)
BW_ADD_TEST_CASE(redis_article_adapter_test article_main_service)
BW_ADD_TEST_CASE(thrift_timer_manager_test thrift)
BW_ADD_TEST_CASE(thrift_thread_manager_test thrift)
BW_ADD_TEST_CASE(thrift_client_pool_test thrift_ArticleMain thrift_ServiceBase base)

BW_ADD_EXECUTABLE(service_runner service_runner dl)
BW_ADD_EXECUTABLE(article_stat article_stat fcgi_service base)
BW_ADD_EXECUTABLE(link_proxy link_proxy fcgi_service base boost_system)
BW_ADD_EXECUTABLE(fcgi_demo fcgi_demo sns fcgi_service ctemplate)

BW_ADD_EXECUTABLE(search_engine search_engine thrift_SearchEngine fcgi_service mmseg base)
BW_ADD_EXECUTABLE(related_recomm related_recomm thrift_RelatedRecomm service_arch base boost_thread)
# BW_ADD_EXECUTABLE(redis_client redis_client base hiredis)

# BW_ADD_EXECUTABLE(twitter twitter_service thrift_TwitterService service_arch)
BW_ADD_EXECUTABLE(article_comment article_comment_service thrift_ArticleComment database redis_executor service_arch)
# BW_ADD_EXECUTABLE(article_main article_main_service thrift_ArticleMain database redis_executor service_arch)
BW_ADD_EXECUTABLE(generic_counter/service generic_counter_service thrift_GenericCounter database redis_executor service_arch)
BW_ADD_EXECUTABLE(favorite_service favorite_service thrift_FavoriteService database redis_executor service_arch)

BW_ADD_SHARED_LIBRARY(twitter twitter_service thrift_TwitterService thrift_ServiceBase)
#BW_ADD_SHARED_LIBRARY(article_comment article_comment_service thrift_ArticleComment database redis_executor service_arch)
BW_ADD_SHARED_LIBRARY(article_main article_main_service thrift_ArticleMain database redis_executor service_arch)

